import 'package:grocery_app_demo/api/entity/entity_factory.dart';

class ScmRes<R> {
  dynamic data;
  int? code;
  String? msg;

  bool get success => 0 == code;

  ScmRes({this.data, this.code, this.msg});

  @override
  String toString() {
    return 'ScmRes{data: $data, code: $code, msg: $msg}';
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['code'] = code;
    data['data'] = data;
    data['msg'] = msg;
    return data;
  }

  ScmRes.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    data = json['data'];
    msg = json['msg'];
  }

  /// 获取results数组
  T getResults<T>() {
    return EntityFactory.generateOBJ<T>(data);
  }
}

class ScmResList<R> {
  List<dynamic>? data;
  int? code;
  String? msg;

  bool get success => 0 == code;

  ScmResList({this.data, this.code, this.msg});

  @override
  String toString() {
    return 'ScmResList{data: $data, status: $code, message: $msg}';
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['code'] = code;
    data['data'] = data;
    data['msg'] = msg;
    return data;
  }

  ScmResList.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    data = json['data'];
    msg = json['msg'];
  }

  /// 获取results数组
  List<T> getList<T>() {
    late List<T> list = [];
    if (data != null && data!.isNotEmpty) {
      for (var value in data!) {
        list.add(EntityFactory.generateOBJ<T>(value));
      }
    }
    return list;
  }
}
